/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package util;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.CharUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.validator.EmailValidator;

/**
 *
 * @author WangShuai
 */
public class ValidateUtil {

    /**
     * 检查src中是否包含字符串ext
     * @param src
     * @param ext
     * @return
     */
    public static boolean validateInclude(String src, String ext) {
        src = src.toUpperCase();
        ext = ext.toUpperCase();
        String[] srcArray = src.split(";");
        List<String> srcList = Arrays.asList(srcArray);
        if (srcList.contains(ext)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 验证列表中是否包含src
     * @param target
     * @param src
     * @return
     */
    public static boolean validateInclude(List<Object> target, Object... src) {
        if (src == null || src.length < 1 || target == null || target.isEmpty()) {
            return false;
        }
        for (Object o : src) {
            if (target.contains(o)) {
                return true;
            }
        }
        return false;
    }

    public static boolean validateIncludeKey(Map target, Object... src) {
        if (src == null || src.length < 1 || target == null || target.isEmpty()) {
            return false;
        }
        for (Object o : src) {
            if (target.containsKey(o)) {
                return true;
            }
        }
        return false;
    }

    public static boolean validateInclude(Object target, Object[] src) {
        if (src == null || src.length < 1) {
            return false;
        }
        for (Object i : src) {
            if (target.equals(i)) {
                return true;
            }
        }
        return false;
    }

    public static boolean validateIncludeSimple(Object target, Object... src) {
        if (src == null || src.length < 1) {
            return false;
        }
        for (Object i : src) {
            if (target.equals(i)) {
                return true;
            }
        }
        return false;
    }

    public static boolean validateExclude(String src, String ext) {
        src = src.toUpperCase();
        ext = ext.toUpperCase();
        String[] srcArray = src.split(";");
        List<String> srcList = Arrays.asList(srcArray);
        if (srcList.contains(ext)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 验证字符串是否已否些字符串结尾
     * @param ends    验证依据
     * @param target   要验证的字符串
     * @return
     */
    public static boolean validateEnds(String ends, String target) {
        ends = ends.toUpperCase();
        target = target.toUpperCase();
        String[] endArray = ends.split(";");
        for (String end : endArray) {
            if (target.endsWith(end)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 验证数组是否为null和空
     * @param src
     * @return
     */
    public static boolean isNotNullAndNotEmpty(Object[] src) {
        if (src != null && src.length > 0) {
            return true;
        }
        return false;
    }

    /**
     * 验证List是否为null和空
     * @param src
     * @return
     */
    public static boolean isNotNullAndNotEmpty(List list) {
        if (list != null && !list.isEmpty()) {
            return true;
        }
        return false;
    }

    public static boolean isNotBlank(String src) {
        return StringUtils.isNotBlank(src);
    }

    public static boolean isBlank(String src) {
        return StringUtils.isBlank(src);
    }

    public static boolean isNotBlankAndEqTrue(String src) {
        if (StringUtils.isBlank(src)) {
            return false;
        }
        if ("true".equalsIgnoreCase(src)) {
            return true;
        }
        return false;
    }

    public static boolean isNotNullAndGtZero(Integer target) {
        if (null == target || target.intValue() <= 0) {
            return false;
        }
        return true;
    }

    public static boolean isNotBlankAndEqStr(String src, String eq) {
        boolean result = StringUtils.isNotBlank(src);
        if (result) {
            if (!src.trim().equalsIgnoreCase(eq)) {
                result = false;
            }
        }
        return result;
    }

    public static boolean isNotBlankAndNotEqStr(String src, String eq) {
        boolean result = StringUtils.isNotBlank(src);
        if (result) {
            if (src.trim().equalsIgnoreCase(eq)) {
                result = false;
            }
        }
        return result;
    }

    /**
     * 字符串是否为数字
     * @param target
     * @return
     */
    public static boolean isNumber(String target) {
        if (isBlank(target)) {
            return false;
        }
        return target.matches("\\d+");
    }

    public static boolean isNumber(char target) {
        return CharUtils.isAsciiNumeric(target);
    }

    /**
     * 是否为邮箱地址
     * @param target
     * @return
     */
    public static boolean isEmail(String target) {
        EmailValidator v = EmailValidator.getInstance();
        return v.isValid(target);
    }

    public static void main(String[] args) {
    }
}

